home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / amiga / gui / prcgntn1.lha / Precognition / source / EmbossedGadget.c < prev    next >
C/C++ Source or Header  |  1992-12-23  |  13KB  |  500 lines

  1. /* ==========================================================================
  2. **
  3. **                   EmbossedGadget.c
  4. **
  5. ** ⌐1991 WILLISoft
  6. **
  7. ** ==========================================================================
  8. */
  9.  
  10. #include <stdlib.h>
  11. #include <stdio.h>
  12. #include <exec/types.h>
  13. #include <proto/intuition.h>
  14. #include <proto/exec.h>
  15. #include <proto/graphics.h>
  16. #include "AmigaMem.h"
  17.  
  18. #include "EmbossedGadget.h"
  19. #include "EmbossedGadgetClass.h"
  20. #include "minmax.h"
  21. #include "Precognition_Utils.h"
  22.  
  23. #define WAITING 0
  24. #define PRESSED 1
  25.  
  26. #ifdef BUILDER
  27.  
  28. EmbossedGadget *EmbossedGadget_New( EmbossedGadget *self )
  29. {
  30.    EmbossedGadget *new_gadget;
  31.    void GiveItAName( PObject *obj );
  32.  
  33.    if (new_gadget = (EmbossedGadget *) Amalloc( sizeof(EmbossedGadget) ))
  34.    {
  35.        EmbossedGadget_Init( new_gadget,
  36.           self->Location.x, self->Location.y,
  37.           self->Size.x,     self->Size.y,
  38.           self->g.Flags,    self->g.Activation, self->g.GadgetType,
  39.           self->Pens,       Title(self) );
  40.  
  41.       GiveItAName( new_gadget );
  42.    }
  43.    return new_gadget;
  44. }
  45.  
  46. #endif
  47.  
  48. void EmbossedGadget_CleanUp( EmbossedGadget *self )
  49. {
  50.    Afree( self->BoxBorder );
  51.    self->BoxBorder = NULL;
  52. }
  53.  
  54.  
  55. tPoint EmbossedGadget_SetLocation(  EmbossedGadget *self,
  56.                                     PIXELS          LeftEdge,
  57.                                     PIXELS          TopEdge )
  58. {
  59.    Forbid(); /* Don't want Intuition looking at these while we modify em. */
  60.    self->Location.x  = self->g.LeftEdge = LeftEdge;
  61.    self->Location.y  = self->g.TopEdge  = TopEdge;
  62.    Permit();
  63.  
  64.    return self->Location;
  65. }
  66.  
  67.  
  68.  
  69.  
  70. tPoint EmbossedGadget_AskSize( EmbossedGadget *self,
  71.                                PIXELS          Width,
  72.                                PIXELS          Height )
  73. {
  74.    tPoint size;
  75.  
  76.    size.x = MAX( Width,  6 );
  77.    size.y = MAX( Height, 6 );
  78.  
  79.    return size;
  80. }
  81.  
  82.  
  83. tPoint EmbossedGadget_SetSize( EmbossedGadget *self,
  84.                                PIXELS          Width,
  85.                                PIXELS          Height )
  86. {
  87.    tPoint size;
  88.  
  89.    size = AskSize( self, Width, Height );
  90.  
  91.    Forbid(); /* Don't want Intuition looking at these while we modify em. */
  92.    self->Size     = size;
  93.    self->g.Width  = size.x;
  94.    self->g.Height = size.y;
  95.  
  96.    pcg_Init3DBox(  self->BoxBorder, 0, 0, size.x, size.y,
  97.                    self->Pens.BrightPen,  self->Pens.DarkPen,
  98.                    self->BoxBorder->BottomRight.NextBorder );
  99.    Permit();
  100.  
  101.    AlignText( &self->LabelText, Size(self) );
  102.  
  103.    return size;
  104. }
  105.  
  106.  
  107. Gadget *EmbossedGadget_FirstGadget( EmbossedGadget *self )
  108. {
  109.    return &self->g;
  110. }
  111.  
  112.  
  113. USHORT EmbossedGadget_nGadgets( EmbossedGadget *self )
  114. {
  115.    return 1;
  116. }
  117.  
  118.  
  119. ULONG EmbossedGadget_IDCMPFlags( EmbossedGadget *self )
  120. {
  121.    ULONG flags;
  122.    UWORD activation;
  123.  
  124.    activation = self->g.Activation;
  125.    flags =    ((activation & GADGIMMEDIATE) ? GADGETDOWN :0)
  126.             | ((activation & RELVERIFY)     ? GADGETUP   :0)
  127.             | ((activation & FOLLOWMOUSE)   ? MOUSEMOVE  :0);
  128.    return flags;
  129. }
  130.  
  131.  
  132. USHORT EmbossedGadget_ClaimEvent( EmbossedGadget *self,
  133.                                   IntuiMessage   *event )
  134. {
  135.    USHORT response = 0;
  136.  
  137.    switch (event->Class)
  138.    {
  139.       case REFRESHWINDOW:
  140.          response = RESPONDED;
  141.          break;
  142.  
  143.       case GADGETDOWN:
  144.       case GADGETUP:
  145.          if (event->IAddress == (void*) &self->g)
  146.          {
  147.             response = RESPONDED | CONSUMED_EVENT | CHANGED_STATE;
  148.          }
  149.          break;
  150.  
  151.       case MOUSEBUTTONS: /* Duration events. */
  152.       case INTUITICKS:
  153.          if (self->state)
  154.          {
  155.             response = RESPONDED | CHANGED_STATE;
  156.          }
  157.          break;
  158.    }
  159.  
  160.    return response;
  161. }
  162.  
  163.  
  164. USHORT EmbossedGadget_Respond( EmbossedGadget   *self,
  165.                                IntuiMessage     *Event )
  166. {
  167.    USHORT response = 0;
  168.    Window *window;
  169.  
  170.    switch (Event->Class)
  171.    {
  172.       case REFRESHWINDOW:
  173.          Refresh( self );
  174.          response = RESPONDED;
  175.          break;
  176.  
  177.       case GADGETDOWN:
  178.          if (Event->IAddress == (void*) FirstGadget(self))
  179.          {
  180.             if (self->g.Activation & GADGDURATION)
  181.             {
  182.                window = Event->IDCMPWindow;
  183.                self->state = PRESSED;
  184.                /* Save the original IDCMP settings. */
  185.                self->IDCMPbuf = window->IDCMPFlags;
  186.  
  187.                if (! (self->g.Activation & FOLLOWMOUSE))
  188.                {
  189.                   /*
  190.                   ** For Duration gadgets, on GADGETDOWN, INTUITICKS
  191.                   ** are turned on so the EmbossedGadget can get
  192.                   ** continuous messages.
  193.                   */
  194.                   ModifyIDCMP( window,
  195.                         window->IDCMPFlags | INTUITICKS | MOUSEBUTTONS );
  196.                }
  197.             }
  198.             response = RESPONDED | CONSUMED_EVENT | CHANGED_STATE;
  199.          }
  200.          break;
  201.  
  202.       case GADGETUP:
  203.          if (Event->IAddress == (void*) FirstGadget(self))
  204.          {
  205.             window = Event->IDCMPWindow;
  206.             if (self->state) /* Duration turned on */
  207.             {
  208.                self->state = WAITING;
  209.                ModifyIDCMP( window, self->IDCMPbuf );
  210.             }
  211.  
  212.             response = RESPONDED | CONSUMED_EVENT | CHANGED_STATE;
  213.          }
  214.          break;
  215.  
  216.       case INTUITICKS:
  217.          if (self->state)
  218.          {  /* User is still pressing gadget */
  219.             response = RESPONDED | CHANGED_STATE;
  220.          }
  221.          break;
  222.  
  223.       case MOUSEBUTTONS:
  224.          /* MouseButtons happen when the User moves the mouse off
  225.           * off the gadget before releasing.
  226.           */
  227.          if (self->state)
  228.          {
  229.             window      = Event->IDCMPWindow;
  230.             self->state = WAITING;
  231.             ModifyIDCMP( window, self->IDCMPbuf );
  232.  
  233.             response = RESPONDED | CHANGED_STATE;
  234.          }
  235.          break;
  236.  
  237.       case MOUSEMOVE:
  238.          if (self->state)
  239.          {
  240.             response = RESPONDED | CONSUMED_EVENT | CHANGED_STATE;
  241.          }
  242.          break;
  243.  
  244.    }
  245.    return response;
  246. }
  247.  
  248.  
  249. void EmbossedGadget_Refresh( EmbossedGadget *self )
  250. {
  251.    pcgWindow *window;
  252.    RastPort  *rport;
  253.    Point loc;
  254.  
  255.    if (window = InteractorWindow(self))
  256.    {
  257.       RefreshGList( FirstGadget(self), iWindow(window), NULL,
  258.                      nGadgets(self) );
  259.       if (self->LabelText.IText)
  260.       {
  261.          rport = RPort( window );
  262.          loc   = Location(self);
  263.          PrintIText( rport, &self->LabelText, loc.x, loc.y );
  264.       }
  265.    }
  266. }
  267.  
  268. void EmbossedGadget_Render( EmbossedGadget *self, RastPort *rport )
  269. {
  270.    PIXELS x, y;
  271.    Point loc;
  272.  
  273.    x = self->g.LeftEdge;
  274.    y = self->g.TopEdge;
  275.  
  276.    if (self->g.GadgetRender)
  277.    {
  278.       if (self->g.Flags & GADGIMAGE)
  279.       {
  280.          DrawImage( rport, (struct Image *) self->g.GadgetRender, x, y );
  281.       }
  282.       else
  283.       {
  284.          DrawBorder( rport, (struct Border *) self->g.GadgetRender, x, y );
  285.       }
  286.    }
  287.  
  288.    if (self->g.GadgetText)
  289.    {
  290.       PrintIText( rport, self->g.GadgetText, x, y );
  291.    }
  292.  
  293.    if (self->LabelText.IText)
  294.    {
  295.       loc = Location(self);
  296.       PrintIText( rport, &self->LabelText, loc.x, loc.y );
  297.    }
  298. }
  299.  
  300.  
  301. BOOL EmbossedGadget_EnableIactor( EmbossedGadget *self,
  302.                             BOOL            enable )
  303. {
  304.    struct pcgWindow *window;
  305.    struct Window    *iwindow;
  306.    RastPort *rport;
  307.    Gadget *g;
  308.    PIXELS xmin, xmax, ymin, ymax;
  309.    short i, ngadgets;
  310.  
  311.    window   = InteractorWindow(self);
  312.    g        = FirstGadget(self);
  313.    ngadgets = nGadgets(self);
  314.  
  315.    if (enable)
  316.    {
  317.       if (window && (rport = RPort(window))) /* Window is open */
  318.       {
  319.          for ( i=0; i<ngadgets; i++, g=g->NextGadget)
  320.          {
  321.             xmin  = g->LeftEdge;
  322.             ymin  = g->TopEdge;
  323.             xmax  = xmin + g->Width - 1;
  324.             ymax  = ymin + g->Height - 1;
  325.  
  326.             OnGadget( g, window->Window, NULL );
  327.  
  328.             SetDrMd( rport, JAM1 );
  329.             SetAPen( rport, self->Pens.BackPen );
  330.             RectFill( rport, xmin, ymin, xmax, ymax );
  331.             Refresh(self);
  332.          }
  333.       }
  334.       else
  335.       {
  336.          for ( i=0; i<ngadgets; i++, g=g->NextGadget)
  337.          {
  338.             g->Flags &= (~ GADGDISABLED);
  339.          }
  340.       }
  341.    }
  342.    else
  343.    {
  344.       if (window && (iwindow = iWindow(window))) /* window is open. */
  345.       {
  346.          for ( i=0; i<ngadgets; i++, g=g->NextGadget)
  347.          {
  348.             OffGadget( g, iwindow, NULL );
  349.          }
  350.       }
  351.       else
  352.       {
  353.          for ( i=0; i<ngadgets; i++, g=g->NextGadget)
  354.          {
  355.             g->Flags |= GADGDISABLED;
  356.          }
  357.       }
  358.    }
  359.  
  360.    return enable;
  361. }
  362.  
  363.  
  364. BOOL EmbossedGadget_isEnabled( Interactor *self,
  365.                                BOOL       enable )
  366. {
  367.    Gadget *g;
  368.  
  369.    if (g = FirstGadget(self))
  370.       return (BOOL) (! (g->Flags & GADGDISABLED));
  371.    else
  372.       return FALSE;
  373. }
  374.  
  375.  
  376. char *EmbossedGadget_Title( EmbossedGadget *self )
  377. {
  378.    return (char *) self->LabelText.IText;
  379. }
  380.  
  381. BOOL EmbossedGadget_SetTitle( EmbossedGadget *self,
  382.                               char           *title )
  383. {
  384.    Afree(self->LabelText.IText);
  385.    self->LabelText.IText  = Astrdup(title);
  386.    AlignText( &self->LabelText, Size(self) );
  387.  
  388.    return TRUE;
  389. }
  390.  
  391.  
  392. AlignInfo EmbossedGadget_TextAlignment( EmbossedGadget *self )
  393. {
  394.    return self->LabelText.alignment;
  395. }
  396.  
  397. AlignInfo EmbossedGadget_SetTextAlignment( EmbossedGadget *self,
  398.                                            UBYTE     Flags,
  399.                                            BYTE      Xpad,
  400.                                            BYTE      Ypad )
  401. {
  402.    self->LabelText.alignment.Flags = Flags;
  403.    self->LabelText.alignment.Xpad  = Xpad;
  404.    self->LabelText.alignment.Ypad  = Ypad;
  405.    AlignText( &self->LabelText, Size(self) );
  406.    return self->LabelText.alignment;
  407. }
  408.  
  409.  
  410. BOOL EmbossedGadget_elaborated = FALSE;
  411.  
  412. struct InteractorClass EmbossedGadget_Class;
  413.  
  414. void EmbossedGadgetClass_Init( struct InteractorClass *class )
  415. {
  416.    InteractorClass_Init( class );
  417.    class->isa         = InteractorClass();
  418.    class->ClassName   = "EmbossedGadget";
  419.    class->CleanUp     = EmbossedGadget_CleanUp;
  420.    class->SetLocation = EmbossedGadget_SetLocation;
  421.    class->SetSize     = EmbossedGadget_SetSize;
  422.    class->AskSize     = EmbossedGadget_AskSize;
  423.    class->Render      = EmbossedGadget_Render;
  424.    class->FirstGadget = EmbossedGadget_FirstGadget;
  425.    class->nGadgets    = EmbossedGadget_nGadgets;
  426.    class->IDCMPFlags  = EmbossedGadget_IDCMPFlags;
  427.    class->ClaimEvent  = EmbossedGadget_ClaimEvent;
  428.    class->Respond     = EmbossedGadget_Respond;
  429.    class->Refresh     = EmbossedGadget_Refresh;
  430.    class->EnableIactor= EmbossedGadget_EnableIactor;
  431.    class->isEnabled   = EmbossedGadget_isEnabled;
  432.    class->Activate    = NULL;
  433.    class->isActive    = NULL;
  434.    class->Title       = EmbossedGadget_Title;
  435.    class->SetTitle    = EmbossedGadget_SetTitle;
  436.    class->TextAlignment = EmbossedGadget_TextAlignment;
  437.    class->SetTextAlignment = EmbossedGadget_SetTextAlignment;
  438. }
  439.  
  440.  
  441. struct InteractorClass *EmbossedGadgetClass( void )
  442. {
  443.    if (! EmbossedGadget_elaborated)
  444.    {
  445.       EmbossedGadgetClass_Init( &EmbossedGadget_Class );
  446.       EmbossedGadget_elaborated = TRUE;
  447.    }
  448.  
  449.    return &EmbossedGadget_Class;
  450. }
  451.  
  452.  
  453.  
  454. void EmbossedGadget_Init( EmbossedGadget *self,
  455.                           SHORT           LeftEdge,
  456.                           SHORT           TopEdge,
  457.                           SHORT           Width,
  458.                           SHORT           Height,
  459.                           USHORT          Flags,
  460.                           USHORT          Activation,
  461.                           USHORT          GadgetType,
  462.                           pcg_3DPens      Pens,
  463.                           char           *Label )
  464. {
  465.    Interactor_Init( self );
  466.  
  467.    self->isa             = EmbossedGadgetClass();
  468.    self->Pens            = Pens;
  469.    self->BoxBorder       = (pcg_3DBox *) Acalloc( 1, sizeof( pcg_3DBox ) );
  470.    self->state           = WAITING;
  471.  
  472.    self->LabelText.FrontPen  = Pens.FrontPen;
  473.    self->LabelText.BackPen   = Pens.BackPen;
  474.    self->LabelText.DrawMode  = JAM1;
  475.    self->LabelText.ITextFont = &pcg_Topaz80;
  476.    self->LabelText.IText     = NULL;
  477.    self->LabelText.NextText  = NULL;
  478.    self->LabelText.alignment.Flags = tx_INSIDE | tx_LEFT | tx_YCENTER;
  479.    self->LabelText.alignment.Xpad  = STD_XPAD;
  480.    self->LabelText.alignment.Ypad  = STD_YPAD;
  481.  
  482.    self->g.NextGadget    = NULL;
  483.    self->g.Flags         = Flags;
  484.    self->g.Activation    = Activation;
  485.    self->g.GadgetType    = GadgetType;
  486.    self->g.GadgetRender  = (APTR) &self->BoxBorder->TopLeft;
  487.    self->g.SelectRender  = NULL;
  488.    self->g.MutualExclude = 0L;
  489.    self->g.SpecialInfo   = NULL;
  490.    self->g.GadgetID      = 0;
  491.    self->g.UserData      = NULL;
  492.    self->g.GadgetText    = NULL;
  493.  
  494.    SetLocation( self, LeftEdge, TopEdge );
  495.    SetSize( self, Width, Height );
  496.    SetTitle( self, Label );
  497.  
  498. }
  499.  
  500.